'use strict';
(function (exports) {
    var OutCards = cc.Layer.extendEX({
        ctor: function () {
            this._super();

            this.removeAllChildren();
            return this;
        },
        setPos: function (pos) {
            this.pos = pos;
            mAction.outCards[this.pos] = [];
        },

        addCard: function (cardNo, animation, showCard) {
            var cardList = mAction.outCards[this.pos];
            cardList.push(cardNo);

            var card = new Card();
            this.addChild(card);
            // card.setScale(0.8);
            card.setData(cardNo, null, 2);

            //card.setGrayCard(true);

            var x = 5;
            if (this.pos == 0)
                x = 5;
            if (gameData.playerNum == 4) {
                x = 4;
            }
            if (gameData.playerNum == 2)
                x = 7;

            var rowIndex = parseInt((cardList.length - 1) / x);
            var colIndex = (cardList.length - 1) % x;
            var pos = null;
            var frompos = null;
            if (gameData.playerNum == 3) {
                if (this.pos == 1) {
                    pos = cc.p(150 - colIndex * 45, 15 - rowIndex * 45);
                    frompos = cc.p(780, 320);
                } else if (this.pos == 2) {
                    pos = cc.p(colIndex * 45, 15 - rowIndex * 45);
                    frompos = cc.p(260, 320);
                } else if (this.pos == 0) {
                    pos = cc.p(150 - colIndex * 45, 15 + rowIndex * 45);
                    frompos = cc.p(510, 320);
                }
            } else if (gameData.playerNum == 2) {
                if (this.pos == 1) {
                    pos = cc.p(150 - colIndex * 45, 15 - rowIndex * 45);
                    frompos = cc.p(780, 320);
                } else if (this.pos == 0) {
                    pos = cc.p(20 + colIndex * 45, 15 - rowIndex * 45);
                    frompos = cc.p(510, 320);
                }
            } else {
                if (this.pos == 1) {
                    pos = cc.p(145 - colIndex * 45, rowIndex * 45);
                    frompos = cc.p(780, 320);
                } else if (this.pos == 2) {
                    pos = cc.p(145 - colIndex * 45, rowIndex * 45);
                    frompos = cc.p(260, 320);
                } else if (this.pos == 0) {
                    pos = cc.p(colIndex * 45 + 25, rowIndex * 45);
                    frompos = cc.p(510, 320);
                } else {
                    pos = cc.p(colIndex * 45 + 25, rowIndex * 45);
                    frompos = cc.p(510, 320);
                }
            }
            card.setPosition(pos);
            var worldpos = card.convertToWorldSpace(cc.p(0, 0));
            card.setPosition(cc.p(pos.x - (worldpos.x - frompos.x), pos.y - (worldpos.y - frompos.y)));
            if (animation) {
                if (showCard) {
                    showCard.runAction(cc.sequence(
                        cc.delayTime(0.6)
                        , cc.scaleTo(0.1, 0.01)
                    ));
                }
                card.runAction(cc.sequence(
                    cc.delayTime(0.7)
                    , cc.moveTo(0.2, pos.x, pos.y)
                    // ,cc.scaleTo(0.1, 1.2)
                    // ,cc.scaleTo(0.1, 0.8)
                ));
            } else {
                card.setPosition(pos);
            }
            card.setOutCardray();
        },
        resetCard: function () {
            var cardList = this.getChildren();
            for (var i = 0; i < cardList.length; i++) {
                var card = cardList[i];
                card.setData(cardList[i], null, 2);
            }
        },

    });
    exports.OutCards = OutCards;
})(window);